package com.atguigu.service.impl;

import com.atguigu.Bean.*;
import com.atguigu.dao.OrderDao;
import com.atguigu.dao.OrderItemDao;
import com.atguigu.dao.impl.OrderDaoImpl;
import com.atguigu.dao.impl.OrderItemDaoImpl;
import com.atguigu.service.BookService;
import com.atguigu.service.OrderService;

import java.util.Date;
import java.util.List;
import java.util.Map;

public class OrderServiceImpl implements OrderService {
    private OrderDao od=new OrderDaoImpl();
    private OrderItemDao oid=new OrderItemDaoImpl();
    private BookService bs=new BookServiceImpl();

    /**
     * 交易后保存订单和订单项(为处理因库存不够而显示的购买后库存呈负数的bug)
     * @param c_cart
     * @param userId
     * @return
     */
    @Override
    public String createOrder(C_cart c_cart, Integer userId) {
        String orderId=System.currentTimeMillis()+"-"+userId;
        //生成订单号(唯一)
    /*保存订单*/
        C_order c_order = new C_order(orderId, new Date(), c_cart.getTotalPrice(), 0, userId);
        od.saveOrder(c_order);
    /*保存订单项*/
        //遍历购物车中每一个商品项转换成订单项保存到数据库中
        Map<Integer, C_cartItem> items = c_cart.getItems();
        //一个账号会买多种商品，故用循环
        for (Map.Entry<Integer,C_cartItem>entry:items.entrySet()){
            C_cartItem c_cartItem = entry.getValue();
            //生成每个图书的id
            Integer id = c_cartItem.getId();
            //根据id查询C_book的信息
            C_book c_book = bs.queryBookById(id);
            c_book.setSales(c_book.getSales()+c_cartItem.getCount());
            //通过bookService修改销量
            if(c_book.getStock()<c_cartItem.getCount()){
                int i=12/0;
            }
            c_book.setStock(c_book.getStock()-c_cartItem.getCount());
            //通过bookService修改库存
            bs.updateBook(c_book);
            C_orderItem c_orderItem=new C_orderItem(null,c_cartItem.getName(),c_cartItem.getCount(),c_cartItem.getPrice(),c_cartItem.getTotalPrice(),orderId);
            oid.saveOrderItem(c_orderItem);
        }
        c_cart.clear();
        //清空购物车
        return orderId;
    }

    /**
     * 交易后用户查看【我的订单】
     * @param user_id
     * @return
     */
    @Override
    public C_page<C_myOrder> showMyOrders(Integer user_id,int pageNo,int pageSize) {
        C_page<C_myOrder> pm=new C_page<>();
        //插入当前当前显示页码数
        pm.setPageSize(pageSize);
        Integer pageTotalcount=od.showMyOrdersCount(user_id);
        //插入总记录数
        pm.setPageTotalCount(pageTotalcount);
        Integer pageTotal=pageTotalcount/pageSize;
        if(pageTotalcount%pageSize!=0){
            pageTotal++;
        }
        //插入总页码数
        pm.setPageTotal(pageTotal);
        //插入当前页面
        pm.setPageNo(pageNo);
        //根据当前页码和每页数据数算出sql语句limit语句中的起始坐标begin
        int begin=(pm.getPageNo()-1)*pageSize;
        List<C_myOrder> c_myOrders = od.queryOrdersByUserId(user_id, begin, pageSize);
        //插入当前页码数据
        pm.setItems(c_myOrders);
        return pm;
    }

    /**
     * 查找所有购买过商品的用户信息
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public C_page<C_user> showAllOrderUsers(int pageNo, int pageSize) {
        C_page<C_user> pm=new C_page<>();
        //插入当前当前显示页码数
        pm.setPageSize(pageSize);
        Integer pageTotalcount=od.queryOrderUsersCount();
        //插入总记录数
        pm.setPageTotalCount(pageTotalcount);
        Integer pageTotal=pageTotalcount/pageSize;
        if(pageTotalcount%pageSize!=0){
            pageTotal++;
        }
        //插入总页码数
        pm.setPageTotal(pageTotal);
        //插入当前页面
        pm.setPageNo(pageNo);
        //根据当前页码和每页数据数算出sql语句limit语句中的起始坐标begin
        int begin=(pm.getPageNo()-1)*pageSize;
        List<C_user> c_users = od.queryOrderUsers(begin, pageSize);
        //插入当前页码数据
        pm.setItems(c_users);
        return pm;
    }
    /*
    管理修改订单状态为发货
     */
    @Override
    public void sendOrder(String order_id) {
        int i = od.changeOrderStatus(order_id,1);
        if(i==-1){
            System.out.println("订单发货失败");
        }else {
            System.out.println("订单发货成功");
        }
    }
    /*
    用户修改订单状态为签收
     */
    public void receiverOrder(String order_id){
        int i = od.changeOrderStatus(order_id, 2);
        if(i==-1){
            System.out.println("订单签收失败");
        }else {
            System.out.println("订单签收成功");
        }
    }
}
